<?php

namespace App\Trip\Plane\Variflight;

use App\Trip\Plane\Requests\CommonQueryInterface;
use App\Trip\Plane\Variflight\Result\ComfortResult;
use App\Trip\Plane\Variflight\Result\ComfortResultInterface;
use Exception;
use GuzzleHttp\Exception\GuzzleException;

class FlightComfort extends Variflight
{

    /**
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->business = 'comfort';
        $this->url      = config('trip.airplane.variflight.comfort.api_url');
    }

    /**
     * 按城市 对 和日期查询
     * @throws Exception|GuzzleException
     */
    public function queryByDateAndCity(CommonQueryInterface $request): ComfortResultInterface
    {
        $param = [
            'depCity' => $request->getFrom()->getCityCode(),
            'arrCity' => $request->getTo()->getCityCode(),
            'date'    => $request->getTripDate(),
        ];

        return new ComfortResult($this->query($param));
    }


    /**
     * 按航班号+日期查询
     * @throws GuzzleException
     * @throws Exception
     */
    public function queryByFlightNo(VariflightRequest $request): ComfortResultInterface
    {
        $param = [
            'fnum' => $request->getFlightNo(),
            'date' => $request->getTripDate(),
        ];

//        // 这里先耦合吧 todo
//        $comfort      = new PlaneComfort();
//        $comfort_info = $comfort->findByFlight($request->getFlightNo());
//        // 如果数据库查询到的数据 用db
//        if ($comfort_info && Carbon::now()->diffInDays($comfort_info->update_at) < 10) {
//            return new QueryResultLocal($comfort_info);
//        }

        $from = $request->getFrom();
        $to   = $request->getTo();
        if ($from->getType() != $to->getType() || $to->getType() != 2) {
            throw new Exception("当前类型仅支持传入机场三字码");
        }
        return new ComfortResult($this->query($param), [
            'from_code' => $from->getCode(),
            'to_code'   => $to->getCode(),
        ]);
    }

    /**
     * @throws Exception
     * @throws GuzzleException
     */
    public function queryByDateAndAirport(CommonQueryInterface $request): ComfortResultInterface
    {
        $param = [
            'dep'  => $request->getFrom()->getCode(),
            'arr'  => $request->getTo()->getCode(),
            'date' => $request->getTripDate(),
        ];

        return new ComfortResult($this->query($param));
    }
}